import java.util.ArrayList;
import java.util.List;




public class ENTTable {
	
	//store symbols in the ENT operands.
	private static List<String> entTab = new ArrayList<String>();
	
	
	public static void add(String str,Integer lineNum) throws AssembleException {
		if(entTab.contains(str)) {
			throw new AssembleException(16,lineNum,str);
		}else {
			entTab.add(str);
		}
	}
	
	
	//convert each element in the table to the object code format.
	public static String toObj() {
		int len = entTab.size();
		StringBuilder objString = new StringBuilder();
		objString.append("I"+Utilities.intToHex(Lines.getStartingLoc())+Lines.getSegName()+'\n');
		for(int i =1;i<len;i++) {
			objString.append("I"+ Utilities.intToHex(SymbTable.getVal(entTab.get(i)))
					+entTab.get(i)+"\n");
		}
		return objString.toString();
	}
	
	//check if all symbols in the table defined in the local segment.otherwise throw exception.
	public static void checkValid() throws AssembleException {
		for(String val:entTab) {
			if(!SymbTable.hasSym(val) && !val.equals(Lines.getSegName())) {
				throw new AssembleException(0,"Symbol "+val+" in the .ENT operands is not defined in the local segment");
		}
	}
	
}
	}
